/*
 * =====================================================================================
 *
 *       Filename:  threadApp.cpp
 *
 *    
 *
 *        Version:  1.0
 *        Created:  2012년 06월 01일 12시 04분 59초
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jjlee (), jjlee@nhn.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <iostream>
#include "Runnable.h"
#include "Thread.h"
#include <stdio.h>
#include <errno.h>
#include <string.h>

using namespace std;

class Counter:public Runnable 
{
public:
	Counter(int n = 10): max(n) {}
	void run() 
	{
		cout << Thread::getId() << "] Counter::run() until " << max << endl;

		for (int i = 0; i < max; i++) {
			cout << "Counter::run(): " << i << endl;
			sleep(1);
		}
	}

private:
	int max;
};

class TestThread:public Thread 
{
public:
	void run() 
	{
		for (int i = 0; i < 10; i++) {
			cout << "TestThread::run(): " << i << endl;
			if (i == 5) {
				exit(-1);
			}
			sleep(1);
		}
	}
};


void testWaitUnknownThread()
{
	void *ptr = NULL;
	int ret = pthread_join(0, &ptr);
	if (ret != 0) {
		cerr << "pthread_join exit:"  << strerror(ret) << endl;
	}
}


int main(void)
{
	TestThread tt;
	Thread tr(new Counter(5));

	testWaitUnknownThread();
	tt.start();
	tr.start();

	cout << "exit_code:" << tt.wait() << endl;
	cout << "exit_code:" << tr.wait() << endl;
}

